﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections.Specialized;
using System.Data.Linq;
using IISLogAnalyzer.Model.Entities;

namespace IISLogAnalyzer.Data.Providers
{
    public class SqlDataProvider : DataProvider
    {
        private string _connectionString = string.Empty;

        public override void Initialize(string name, NameValueCollection config)
        {
            base.Initialize(name, config);

            this._connectionString = config["connectionString"];

            if (string.IsNullOrEmpty(this._connectionString))
                throw new ConfigurationErrorsException
                    ("connectionString must be set to the appropriate value");
        }

        public override List<W3CLogging> GetLogFromPeriod(DateTime startDate, DateTime endDate)
        {
            List<W3CLogging> result = null;

            aspnetdbEntities ctx = new aspnetdbEntities(_connectionString);
                
            result = 
                    (from w3 in ctx.W3CLogging 
                     where w3.date >= startDate && w3.date <= endDate 
                     select w3).ToList();   
            
            return result;
        }

        public override List<W3CLogging> GetAllLogData()
        {
            List<W3CLogging> result = null;

            aspnetdbEntities ctx = new aspnetdbEntities(_connectionString);

            result =
                    (from w3 in ctx.W3CLogging                    
                     select w3).ToList();

            return result;
        }

        public override int InsertLogData(List<W3CLogging> logList)
        {
            aspnetdbEntities ctx = new aspnetdbEntities(_connectionString);

            //ctx.W3CLogging.
            
            return 0;
        }

        public override int InsertLogData(W3CLogging logEvent)
        {
            aspnetdbEntities ctx = new aspnetdbEntities(_connectionString);
            int result = 1;

            //dc.W3CLoggings.InsertOnSubmit(logEvent);
  
            //dc.SubmitChanges();

            return result;
        }

        public override int DeleteLogData(W3CLogging logEvent)
        {
            aspnetdbEntities ctx = new aspnetdbEntities(_connectionString);
            int result = 1;

            //dc.W3CLoggings.DeleteOnSubmit(dc.W3CLoggings.Single(w => w.id == logEvent.id));
            //dc.SubmitChanges();

            return result;
        }


        public override List<AnalyzeResult> GetDataByCip()
        {
            aspnetdbEntities ctx = new aspnetdbEntities(_connectionString);
            List<AnalyzeResult> result = new List<AnalyzeResult>();
            AnalyzeResult aitem = null;
            //List<GetDataByCipResult> resultFromSp = dc.GetDataByCip().ToList();

            //if (resultFromSp != null)
            //{
            //    foreach (GetDataByCipResult item in resultFromSp)
            //    {
            //        aitem = new AnalyzeResult();
            //        aitem.Name = item.c_ip;
            //        aitem.Value = (long)item.CipCount;

            //        result.Add(aitem);
            //    }
            //}

            return result;
        }

        public override List<AnalyzeResult> GetDataByUa()
        {
            aspnetdbEntities ctx = new aspnetdbEntities(_connectionString);
            List<AnalyzeResult> result = new List<AnalyzeResult>();
           // AnalyzeResult aitem = null;
            //List<GetDataByUaResult> resultFromSp = dc.GetDataByUa().ToList();

            //if (resultFromSp != null)
            //{
            //    foreach (GetDataByUaResult item in resultFromSp)
            //    {
            //        aitem = new AnalyzeResult();
            //        aitem.Name = item.cs_ua;
            //        aitem.Value = (long)item.UaCount;

            //        result.Add(aitem);
            //    }
            //}

            return result;
        }

        public override List<AnalyzeResult> GetDataByResource()
        {
            aspnetdbEntities ctx = new aspnetdbEntities(_connectionString);
            List<AnalyzeResult> result = new List<AnalyzeResult>();
            //AnalyzeResult aitem = null;
            //List<GetDataByResourceResult> resultFromSp = dc.GetDataByResource().ToList();

            //if (resultFromSp != null)
            //{
            //    foreach (GetDataByResourceResult item in resultFromSp)
            //    {
            //        aitem = new AnalyzeResult();
            //        aitem.Name = item.cs_uri_stem;
            //        aitem.Value = (long)item.CuriCount;

            //        result.Add(aitem);
            //    }
            //}

            return result;
        }


        public override void GetDataPeriod(ref DateTime startDate, ref DateTime endDate)
        {
            throw new NotImplementedException();
        }
    }
}
